package com.tony.pmp.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Joiner;
import com.mysql.cj.log.Log;
import com.tony.pmp.common.response.StatusCode;
import com.tony.pmp.common.utils.CommonUtil;
import com.tony.pmp.common.utils.PageUtil;
import com.tony.pmp.common.utils.QueryUtil;
import com.tony.pmp.model.entity.SysPostEntity;
import com.tony.pmp.model.mapper.SysPostMapper;
import com.tony.pmp.server.service.SysPostService;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: SysPostServiceImpl
 * @Auther: 皮蛋布丁
 * @Date: 2021/07/14/21:49
 * @Description:
 */
@Service("sysPostService")
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPostEntity> implements SysPostService {

    private static final Logger log= LoggerFactory.getLogger(SysPostServiceImpl.class);

    /**
    * @Description: queryPage 获取岗位信息列表(分页)
    * @Param: [params]
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/7/14 21:59
    */
    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        String search = (params.get("search") == null)? "": params.get("search").toString();
        //调用自封装的分页查询工具
        IPage<SysPostEntity> queryPage = new QueryUtil<SysPostEntity>().getQueryPage(params);

        QueryWrapper wrapper = new QueryWrapper<SysPostEntity>()
                .like(StringUtils.isNotBlank(search),"post_code",search.trim())
                .or(StringUtils.isNotBlank(search))
                .like(StringUtils.isNotBlank(search),"post_name",search.trim());

        IPage<SysPostEntity> respage = this.page(queryPage,wrapper);
        return new PageUtil(respage);
    }

    /**
    * @Description: savePost 新增岗位信息
    * @Param: [entity]
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/7/17 23:25
    */
    @Override
    public void savePost(SysPostEntity entity) {
        if (this.getOne(new QueryWrapper<SysPostEntity>().eq("post_code",entity.getPostCode())) != null) {
            throw new RuntimeException(StatusCode.PostCodeHasExist.getMsg());
        }
        entity.setCreateTime(DateTime.now().toDate());
      save(entity);
    }

    /**
    * @Description: updatePost 修改岗位信息
    * @Param: [entity]
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/7/18 22:31
    */
    @Override
    public boolean updatePost(SysPostEntity entity) {
        SysPostEntity old = this.getById(entity.getPostId());
        if (old != null && !old.getPostCode().equals(entity.getPostCode())) {
            if (this.getOne(new QueryWrapper<SysPostEntity>().eq("post_code",entity.getPostCode())) != null) {
                throw  new RuntimeException(StatusCode.PostCodeHasExist.getMsg());
            }
        }
        entity.setUpdateTime(DateTime.now().toDate());
        return updateById(entity);
    }

    /**
    * @Description: deletePatch 批量删除
    * @Param: [ids]
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/7/18 22:58
    */
    @Override
    public void deletePatch(Long[] ids) {
        //第一种写法：mybatis-plus
        //removeByIds(Arrays.asList(ids));

        //第二种写法
        //ids = [1,2,3,4,5]; -> "1,2,3,4,5"
        String delIds = Joiner.on(",").join(ids);
        //ids为int类型
//        log.info("结果：{}",delIds);
//        baseMapper.deleteBatch(delIds);
        //工具类拼接
        baseMapper.deleteBatch(CommonUtil.concatStrToInt(delIds,","));
    }
}
